<?php
	$home = TRUE;
	require_once('include/config.inc.php');
	
	if (isset($_GET['logout']))
	{
		session_destroy();
		header('Location:index.php');
		exit(0);
	}
	
	if (isset($_POST['login']))
	{
		$username = mysql_real_escape_string(strip_tags($_POST['username']));
		$password = mysql_real_escape_string(strip_tags($_POST['password']));
		
		$query = "SELECT * FROM users WHERE username = '$username'";
		$result = mysql_query($query);
		if (mysql_num_rows($result) > 0)
		{
			$row = mysql_fetch_assoc($result);
			
			$user_id = $row['id'];
			$correctPass = $row['password'];
			$attemptedPass = sha1($password);	
			
			if ($attemptedPass == $correctPass)
			{
				$_SESSION['user_id'] = $user_id;
				$_SESSION['login_message'] = "<p style=\"font-weight:bold;\">Successfully logged in!</p>";
				header('Location:index.php');
				exit(0);
			}
			else
			{
				$_SESSION['login_message'] = "<p style=\"font-weight:bold; color:#f00;\">Invalid username/password</p>";
			}
		}
		else
		{
			$_SESSION['login_message'] = "<p style=\"font-weight:bold; color:#f00;\">Invalid username/password</p>";
		}
	}

	require_once('include/top.inc.php');


	if (isset($_SESSION['login_message']))
	{
		echo $_SESSION['login_message'];
		unset($_SESSION['login_message']);
	}

	if (empty($_SESSION['user_id']))
	{?>

		<form name="user_login" action="index.php" method="post">
        	<p style="margin:5px 0px 5px 0px;">Username:<br />
            <input type="text" name="username" class="text_input" maxlength="30" /></p>
            <p style="margin:5px 0px 5px 0px;">Password:<br />
            <input type="password" name="password" class="text_input" max_length="20" /></p>
            <p><input type="submit" name="login" value="Login" /></p>
        </form>
        
    <?php
	}
	else 
	{
		$id = $_SESSION['user_id'];
		$query = "SELECT username FROM users WHERE id = '$id'";
		$result = mysql_query($query);
		$row = mysql_fetch_assoc($result);
		$username = $row['username'];

		?>
        <h1>Welcome <?php echo $username; ?>!</h1>
		
        <p>To use this program, please follow the directions below:</p>
        <ol class="index_list">
        	<li>Click "Enter Data" in the navigation bar above. Enter data for:
            	<ol>
                	<li>Courses</li>
                    <li>Instructors</li>
                    <li>Buildings/Classrooms</li>
                </ol>
            </li>
            <li>Once all data is entered, click "Create Schedules" in the navigation menu.
            	<ol>
                	<li>Select the semester and building which you wish to schedule</li>
                    <li>Click the button to start the scheduling program</li>
                </ol>
            </li>
            <li>Click "View Schdeules" in the navigation menu to view created schedules.
            	<ol>
                	<li>You can go back to a previously created schedule anytime.</li>
                    <li>You can also export a schedule to a spreadsheet document.</li>
                </ol>
            </li>
        </ol>
	<?php	
	}
	?>

<?php
	require_once('include/bottom.inc.php');
?>